<?php
/**
 *
 * 版权所有：顺势网站管理系统<www.51shunshi.com>

 * 日    期：2016-01-21
 * 版    本：1.0.0
 * 功能说明：前台公用控制器。
 *
 **/

namespace Home\Controller;

use Think\Controller;

class ComController extends Controller
{

    public function _initialize()
    {
        C(setting());
        /*
        $links = M('links')->limit(10)->order('o ASC')->select();
        $this->assign('links',$links);
        */
        //热门标签
        $label_db = M("adviser");
        $labellist = $label_db
            ->field("id,name")
            ->select();
        $this->assign("labellist",$labellist);
        //导航栏
        $id = $_GET['id'];
        $category_db = M(category);
        $categorycon= $category_db
            ->field("id,name,dir")
            ->limit(7)
            ->select();
        $this->assign(nav,$categorycon);
        $this->assign(navp,$categorycon);
        $this->assign(navp2,$categorycon);
        $this->assign(navi,$categorycon);
        $this->assign(onclassflag,$id);

    }

    protected function searchBy( $keywords, $p = 1 ){

        if(!empty($keywords)){
            $p = intval($p) > 0 ? $p : 1;
            $pagesize = 10;#每页数量
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $articleDB = M('article as a');
            $orderby = "a.aid DESC";
            $map['a.title'] = array('like',"%".$keywords."%");
            $map['a.description'] = array('like',"%".$keywords."%");
            $map['_logic'] = 'OR';
            $prefix = C('DB_PREFIX');
            $field = "a.aid,a.title,a.description,a.thumbnail,a.t,a.point,a.browse,{$prefix}category.name,{$prefix}category.dir";
            $count = $articleDB->where($map)->count();
            $list = $articleDB
                ->field($field)
                ->where($map)
                ->order($orderby)
                ->join("{$prefix}category ON {$prefix}category.id = a.sid")
                ->limit($offset . ',' . $pagesize)
                ->select();
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $list[$key]['title'] = str_replace($keywords, "<em>" . $keywords . "</em>", $value['title']);
                    $list[$key]['description'] = str_replace($keywords, "<em>" . $keywords . "</em>", $value['description']);
                    $list[$key]['time'] = mdate(strtotime($value['t']));
                    $list[$key]['count'] = $count;
                }
            }else{
                return ["code"=>400, "msg"=>"搜索结果为空"];
            }
            $page = new \Think\Page($count, $pagesize);
            $page = $page->show();
            return ["list"=>$list,"page"=>$page];
        }else{
            return ["code"=>400, "msg"=>"关键词为空"];
        }
    }

    protected function searchLbP( $id, $p = 1 ){
        //点击标签查出所有带此标签的文章
        if(!empty($id)){
            $p = intval($p) > 0 ? $p : 1;
            $pagesize = 10;#每页数量
            $offset = $pagesize * ($p - 1);//计算记录偏移量
            $aPDB = M("adviser_product as ap");
            $prefix = C('DB_PREFIX');
            $field = "{$prefix}article.aid,{$prefix}article.title,{$prefix}article.description,{$prefix}article.thumbnail,{$prefix}article.t,{$prefix}article.point,{$prefix}article.browse,{$prefix}category.dir,{$prefix}category.name";
            $list = $aPDB
                ->where(array("pro_id"=>$id))
                ->field($field)
                ->order("{$prefix}article.aid DESC")
                ->join("{$prefix}article ON {$prefix}article.aid = ap.adviser_id")
                ->join("{$prefix}category ON {$prefix}category.id = {$prefix}article.sid")
                ->limit($offset . ',' . $pagesize)
                ->select();
            $couno = $aPDB
                ->where(array("pro_id"=>$id))
                ->field($field)
                ->order("{$prefix}article.aid DESC")
                ->join("{$prefix}article ON {$prefix}article.aid = ap.adviser_id")
                ->join("{$prefix}category ON {$prefix}category.id = {$prefix}article.sid")
                ->count();
            if(!empty($list)){
                foreach ($list as $key => $value) {
                    $list[$key]['time'] = mdate(strtotime($value['t']));
                    $list[$key]['count'] = $couno;
                }
            }else{
                return ["code"=>400, "msg"=>"搜索结果为空"];
            }
            $page = new \Think\Page($couno, $pagesize);
            $page = $page->show();
            return ["list"=>$list,"page"=>$page];
        }else{
            return ["code"=>400, "msg"=>"关键词为空"];
        }
    }

    /**
     * 最近七天点击最多的栏目文章列表
     * @param string $sid
     */
    protected function hotList($sid = ""){
        //起止时间
        $start_time = date('Y-m-d 00:00:00', strtotime('-7 days'));
        $end_time = date("Y-m-d 23:59:59", time());
        $articleLogDB = M('visit_logs as b');
        $where = " b.visit_time >= '".$start_time."' AND b.visit_time <= '".$end_time."'";
        if(!empty($sid)){
            $where .= " AND b.sid = $sid";
        }
        $prefix = C('DB_PREFIX');
        $list = $articleLogDB
            ->field("b.aid,count(*) as point,b.sid,{$prefix}category.dir")
            ->join("{$prefix}category ON {$prefix}category.id = b.sid")
            ->where($where)
            ->group('b.aid')
            ->order('point DESC')
            ->limit(6)
            ->select();
       return $list;
        //return $articleLogDB->getLastSql();
    }

//获取用户真实IP
    protected function getIp() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else
            if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            else
                if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                    $ip = getenv("REMOTE_ADDR");
                else
                    if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                        $ip = $_SERVER['REMOTE_ADDR'];
                    else
                        $ip = "unknown";
        return ($ip);
    }

}